DATA SEGMENT
    DA DB 00h, 41H, 70H, 03H, 53H, 88H, 16H, 8EH, 3FH
    DC DB 10 DUP(0)
DATA ENDS

CODE SEGMENT
    ASSUME DS:DATA, CS:CODE
START:  MOV AX, DATA
        MOV DS, AX
;*********************3-1************************
        LEA BX, DC
        MOV DI, 00H
        MOV CX, 0AH
;INPUT DC
INDC:   CALL INPUT
        INC DI
        LOOP INDC
        ;SORT DC        
        LEA BX, DC
        MOV DI, 00H
        MOV CX, 09H      
        CALL SORTGL
        ;ENTER
        MOV AH, 02H
        MOV DL, 0DH
        INT 21H
        MOV DL, 0AH
        INT 21H
        ;PRINT DC
        LEA BX, DC
        MOV DI, 00H
        MOV CX, 0AH
        CALL PRINT
        
        MOV AH, 02H
        MOV DL, 0DH
        INT 21H
        MOV DL, 0AH
        INT 21H
        
        MOV AH, 02H
        MOV DL, 0DH
        INT 21H
        MOV DL, 0AH
        INT 21H
        
;*********************3-2************************
        LEA BX, DA
        MOV DI, 01H
        MOV CX, 07H
        CALL SORTGL
        
        MOV DI, 00H
        CALL INPUT
        MOV  CX, 8H
        CALL SORTGL
        MOV DI, 00H
        MOV CX, 09H
        MOV AH, 02H
        MOV DL, 0DH
        INT 21H
        MOV DL, 0AH
        INT 21H
        CALL PRINT
        ;RETURN
        MOV AH, 4CH
        INT 21H

    ;INPUT PROC
    ;TABLE TYPE: BYTE
    ;PARAM BX: TABLE HEAD
    ;PARAM DI: TABLE POSE
    INPUT   PROC NEAR
            PUSH AX
            PUSH DX
            PUSH CX
            MOV AL, 00H
            MOV CL, 4H        
            MOV AH, 01H
            INT 21H
            CALL TRANS           
            SHL AL, CL
            MOV DL, AL
            MOV AH, 01H
            INT 21H
            CALL TRANS
            OR  DL, AL
            MOV [BX+DI], DL
            ;MOV DH, DL
            POP CX
            POP DX
            POP AX
            RET
    INPUT   ENDP

    ;transform char to number
    ;param al: the source char
    TRANS   PROC NEAR
            CMP AL, 'A'
            JB  DIGIT
            CMP AL, 'a'
            JB  UPPER
            SUB AL, 'a'
            ADD AL, 0AH
            JMP EN
    DIGIT:  SUB AL, '0'
            JMP EN
    UPPER:  SUB AL, 'A'
            ADD AL, 0AH
    EN:     RET
    TRANS   ENDP

    ;sort table down
    ;TABLE TYPE: BYTE
    ;param cx: length of table dec
    ;param bx: table head
    ;param di: table pose
    SORTGL  PROC NEAR
            PUSH AX
    SORTA:  PUSH CX 
    SORT:   MOV  AL, BX[DI]
            CMP  AL, BX[DI+1]
            JA   NEX
            XCHG AL, BX[DI+1]
            MOV  BX[DI], AL
    NEX:    INC DI
            LOOP SORT
            POP CX
            MOV DI, 00H
            LOOP SORTA
            POP AX
            RET
    SORTGL  ENDP
              
    ;print a table
    ;TABLE TYPE: BYTE
    ;param bx: table head
    ;param di: table pose
    ;param cx: length of table
    PRINT   PROC NEAR
            PUSH AX
            PUSH DX
            MOV  DL, 00H
    SHOW:   MOV  DH, BX[DI]
            MOV  DL, 0F0H
            PUSH CX
            MOV  CL, 04H        
            AND  DL, DH 
            SHR  DL, CL
            POP CX
            CALL TRANSC
            MOV  AH, 02H
            INT  21H
            MOV  DL, 0FH
            AND  DL, DH
            CALL TRANSC
            INT  21H
            INC  DI
            
            MOV AH, 02H
            MOV dl, ' '
            int 21h
            LOOP SHOW
            POP  DX
            POP  AX
            RET
    PRINT   ENDP

    ;transform number to char
    ;param al:    the source data
    TRANSC  PROC NEAR
            CMP  DL, 0AH
            JB   DIGC
            ADD  DL, 'A'
            SUB  DL, 0AH
            JMP  ENDTC
    DIGC:   ADD  DL, '0'
    ENDTC:  RET
    TRANSC  ENDP
CODE ENDS
END START
        
    
